package cn.youwan.market;


import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONObject;
import cn.youwan.market.adapter.MypagerAdapter;
import cn.youwan.market.entity.AppListBean;
import cn.youwan.market.entity.PromotionListbean.Appbean;
import cn.youwan.market.http.StarHttpClient;
import cn.youwan.market.message.YouwanMessage;
import cn.youwan.market.utils.Const;
import cn.youwan.market.utils.Utils;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ManageActivity extends Activity implements OnClickListener{

	private ViewPager homeViewPager;
	private ImageView jiantou;
	private ArrayList<View> homeListViews;
	private TextView[] texts;
	private LocalActivityManager manager;
	protected int page;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_manager);
		manager = new LocalActivityManager(this, true);
		manager.dispatchCreate(savedInstanceState);
		initHomeViewPager();
		
		//		Appbean bean = new Appbean();
		//		bean.setUrl("http://gd.7pingmi.com.cn/game/apk/hjhdl.apk");
		//		bean.setPkg("com.haox.tank1");
		//		bean.setAppName("测试下载坦克");
		//		
		//		Intent intent = new Intent();	
		//		intent.setAction(Const.START_DOWNLOAD_ACTION);
		//		Bundle bundle = new Bundle();
		//		bundle.putSerializable("bean", bean);
		//		intent.putExtras(bundle);
		//		startService(intent);
		
		getMoreGameRank();
		
		
	}

	private void initHomeViewPager() {
		jiantou = (ImageView) findViewById(R.id.manager_line_img);
		homeViewPager = (ViewPager) findViewById(R.id.manager_viewPager);
		homeListViews = new ArrayList<View>();
		Intent intent1 = new Intent(this, RefreshActivity.class);
		homeListViews.add(getView("RefreshActivity", intent1));
		Intent intent2 = new Intent(this, DownloadActivity.class);
		homeListViews.add(getView("DownloadActivity", intent2));
		Intent intent3 = new Intent(this, DownloadingActivity.class);
		homeListViews.add(getView("DownloadingActivity", intent3));
		Intent intent4 = new Intent(this, UninstallActivity.class);
		homeListViews.add(getView("UninstallActivity", intent4));

		homeViewPager.setAdapter(new MypagerAdapter(homeListViews));
		homeViewPager.setOnPageChangeListener(new HomeViewPageChangeListener());
		texts = new TextView[4];
		texts[0] = (TextView) findViewById(R.id.manager_text1);
		texts[1] = (TextView) findViewById(R.id.manager_text2);
		texts[2] = (TextView) findViewById(R.id.manager_text3);
		texts[3] = (TextView) findViewById(R.id.manager_text4);
		for (TextView t : texts) {
			t.setOnClickListener(this);
		}
		showSelectBar(0);
	}
	// 滑动页面更改事件监听器
	private class HomeViewPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int index) {
			// group.removeAllViews();
			showSelectBar(index);
			switch (index) {
			case 0:
				RefreshActivity act = (RefreshActivity) manager
				.getActivity("RefreshActivity");
				break;
			case 1:
				DownloadActivity act0 = (DownloadActivity) manager
				.getActivity("DownloadActivity");
				break;
			case 2:
				DownloadingActivity act1 = (DownloadingActivity) manager
				.getActivity("DownloadingActivity");
				break;
			case 3:
				UninstallActivity act2 = (UninstallActivity) manager
				.getActivity("UninstallActivity");
				break;
			}
		}
	}

	private View getView(String id, Intent intent) {
		return manager.startActivity(id, intent).getDecorView();
	}


	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.manager_text1) {
			homeViewPager.setCurrentItem(0);
		} else if (v.getId() == R.id.manager_text2) {
			homeViewPager.setCurrentItem(1);
		} else if (v.getId() == R.id.manager_text3) {
			homeViewPager.setCurrentItem(2);
		} else if (v.getId() == R.id.manager_text4) {
			homeViewPager.setCurrentItem(3);
		}
	}



	private void showSelectBar(int index) {
		for (int i = 0; i < texts.length; i++) {
			if (index == i) {
				texts[i].setTextColor(this.getResources().getColor(
						R.color.left_menu));
			} else {
				texts[i].setTextColor(this.getResources().getColor(
						R.color.black_bg));
			}
		}
		int pos = (int) (Utils.getPhoneWidth(this) / 4);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(pos,
				(int) (Utils.getDensity(this) * 4));
		int left = pos * index;
		lp.setMargins(left, (int) (Utils.getDensity(this) * 8), 0, 0);
		jiantou.setLayoutParams(lp);
	}
	
	
	public void getMoreGameRank() {
		new AsyncTask<Void, Void, Void>() {
			protected void onPreExecute() {
			};

			@Override
			protected Void doInBackground(Void... params) {
				YouwanMessage message = new YouwanMessage(ManageActivity.this);
				AppListBean bean = message.getAppsList("http://gd.7pingmi.com.cn/game/server/?m=gameInfo",page);
				return null;
			}

			@Override
			protected void onPostExecute(Void result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				
			}
		}.execute();
	}
	
}
